package com.jkd.analysis.protocol;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.jkd.analysis.alarm.MyAlarm;
import com.jkd.analysis.protocol._interface.EquipmentAgreement;
import com.jkd.taos.domain.SourceData;
import com.jkd.utils.JkConvertUtil;
import com.jkd.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 金码应变协议
 */
@Slf4j
public class JinMaYb implements EquipmentAgreement {

    /**
     * 协议帧最大字节数
     */
    public static final int MAX_FRAME_LEN = 22;
    /**
     * 协议帧正常响应最小字节数
     */
    public static final int MIN_FRAME_LEN = 16;
    /**
     * 帧结构中地址域的字节数
     */
    public static final int ADDRESS_FIELD_LEN = 6;
    /**
     * 帧起始符
     */
    public static final String FRAME_STARTER = "68";
    /**
     * 帧结束符
     */
    public static final String FRAME_END = "16";
    /**
     * 广播帧，当有新设备连接上来后服务端发送此指令和设备进行身份确认
     */
    public static final String BROADCAST_FRAME = "68 AA AA AA AA AA AA 68 11 04 33 33 33 33 AD 16";
    /**
     * 地址域：即设备的出厂编号，智能电表的编号一般在正面的二维码上
     */
    private String addressField;
    /**
     * 控制码
     */
    private String controlCode;
    /**
     * 数据域长度
     */
    private String dataLength;
    /**
     * 数据标识
     */
    private String dataIdentification;
    /**
     * 数据
     */
    private String data;
    /**
     * 校验码
     */
    private String checkSum;

    public JinMaYb() {
    }

    // 旧设备返回  ：$T00005555;::2/C01/9/-27301/-192/748/!
    // 新设备返回  ：&0034222077A;KINGMACH,0000000000,,,,0034222077,49,14.0,*/,+,11,00933868,09,JMZX212HAT,4057.1,2,50.3,2,342.8,9,1065.93,1,,,,,*/,12,00933850,09,JMZX212HAT,3959.3,2,-38.0,2,47.2,9,1053.00,1,,,,,*/,13,00933834,09,JMZX212HAT,3813.8,2,-149.7,2,50.2,9,1033.47,1,,,,,*/,14,00933837,09,JMZX212HAT,4050.6,2,161.0,2,47.7,9,1065.07,1,,,,,*/,15,00933831,09,JMZX212HAT,4008.5,2,14.9,2,46.7,9,1059.53,1,,,,,*/,16,00933857,09,JMZX212HAT,4040.2,2,22.2,2,46.8,9,1063.71,1,,,,,*/,17,00933846,09,JMZX212HAT,4053.1,2,138.3,2,45.9,9,1065.41,1,,,,,*/,18,00933853,09,JMZX212HAT,3749.9,2,-150.5,2,45.0,9,1024.78,1,,,,,*/,19,00933835,09,JMZX212HAT,,2,,2,,9,,,,,,,*/,!
    @Override
    public List<SourceData> analysis(String receiveDataStr, String sensorNum) {
        String toAsc = JkConvertUtil.hexToAscii(receiveDataStr);
        log.info("开始解析应力应变数据，解析前ASCII数据为：{}", toAsc);
        //新旧采集模块返回的格式不同 太钢的是新的采集模块 返回了采集模块编号，旧的没有。
        String modelNum = toAsc.substring(1, 11);
        List<SourceData> list = new ArrayList<>();
        String subStr = StrUtil.subAfter(toAsc, "+,", false);
        String[] split = subStr.substring(0, subStr.length() - 1).split("/,");
        MyAlarm myAlarm = (MyAlarm) SpringUtil.getBean("myAlarm");
        for (String s2 : split) {
            String[] dataS = s2.split(",");
            if (ObjectUtil.isEmpty(dataS[4])) {
                //空值报警
                continue;
            }
//            if (ObjectUtil.isEmpty(dataS[8])) {
//                //温度异常报警
//                continue;
//            }
            SourceData sd = new SourceData();
            sd.setSensorNum(dataS[1]);
//            sd.setDataOne(new BigDecimal(dataS[4]));
            sd.setDataOne(Double.valueOf((dataS[4])));
//            sd.setDataOne(BigDecimal.valueOf(0));
            sd.setUnitOne("με");
//            sd.setDataTwo(Double.valueOf((dataS[7])));
//            sd.setUnitTwo("με");
            if (ObjectUtil.isEmpty(dataS[8])) {
                sd.setTemperature(0.0);
            } else {
                sd.setTemperature(Double.valueOf((dataS[8])));
            }
            sd.setAddTime(DateTime.now().toTimestamp());

            sd.setDataThree(null);
            sd.setVoltage(null);
            sd.setUnitThree(null);
            //判断报警
            myAlarm.judgmentAlarm(dataS[1], Double.valueOf((dataS[4])));
            list.add(sd);
        }
        log.info("应变数据存储完成，采集模块编号:"+modelNum);
        return list;
    }

    public static void main(String[] args) {
        String a = "&0034222077A;KINGMACH,0000000000,,,,0034222077,49,14.1,*/,+,11,00933868,09,JMZX212HAT,4002.2,2,-4.5,2,341.2,9,1058.70,1,,,,,*/,12,00933850,09,JMZX212HAT,3895.4,2,-101.9,2,61.8,9,1044.47,1,,,,,*/,13,00933834,09,JMZX212HAT,3743.8,2,-219.7,2,66.0,9,1023.95,1,,,,,*/,14,00933837,09,JMZX212HAT,4062.1,2,172.5,2,62.0,9,1066.58,1,,,,,*/,15,00933831,09,JMZX212HAT,4030.7,2,37.1,2,60.0,9,1062.46,1,,,,,*/,16,00933857,09,JMZX212HAT,4083.5,2,65.5,2,60.6,9,1069.40,1,,,,,*/,17,00933846,09,JMZX212HAT,4002.2,2,87.4,2,59.6,9,1058.70,1,,,,,*/,18,00933853,09,JMZX212HAT,3841.4,2,-59.0,2,56.5,9,1037.21,1,,,,,*/,19,00933835,09,JMZX212HAT,,2,,2,,9,,,,,,,*/,!";
        String modelNum = a.substring(1, 11);
        String subStr = StrUtil.subAfter(a, "+,", false);
        String[] split = subStr.substring(0, subStr.length() - 1).split("/,");
        System.out.println(split);
    }
}
